package com.example.Android_test.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.activity.training.*;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.util.ReadyAnimations;
import com.example.Android_test.util.TrainingUtils;

public class Main extends Activity {
    public static final String LOG_TAG = "Android_test";
    public static final String TITLE = "SelectSetOfWords";
    public static final boolean IS_RELEASE = false;
    private static Context context;
    private Button main_button_selectWordSet;
    private TextView main_textView_selectedSetOfWordName;
    private Button main_button_goTraining;
    private TextView main_textView_selectedTrainingName;
    private Button main_button_changeTraining;
    private Button main_button_statistic;
    private TextView main_textView_statistic;

    //TextView tvOut;

    public static Context getAppContext() {
        return Main.context;
    }

    @Override
    public void onResume() {
        super.onResume();

/*
        Intent intent = getIntent();
        finish();
        startActivity(intent);*/
    }

    @Override
    public void onRestart() {
        super.onRestart();


/*
        Intent intent = getIntent();
        finish();
        startActivity(intent);*/
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
           /* Intent refresh = new Intent(this, Main.class);
            startActivity(refresh);
            this.finish();*/

            Log.d(Main.LOG_TAG, "Restart Main Activity");

            finish();
            startActivity(getIntent());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_homepage);
        this.setTitle(TITLE);

        Main.context = getApplicationContext();
        //dictionary = new SOWEngine();

        //getWindow().setBackgroundDrawableResource(R.drawable.background_striped_texture);

        //Log.d(Main.LOG_TAG, "main_activity loaded");

        /*
        // TODO
         */
        //SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_WORD_SET_ID, "D001");


        /*String wordSetID = SettingsHelper.INSTANCE.getStringPreference(SettingsHelper.KEY_CURRENT_WORD_SET_ID);
        Log.d(Main.LOG_TAG, "wordSetID " + wordSetID);*/

        main_button_selectWordSet = (Button) findViewById(R.id.ma_buttonSelectSetOfWords);
        main_button_goTraining = (Button) findViewById(R.id.ma_buttonGoTraining);
        main_button_statistic = (Button) findViewById(R.id.ma_buttonStatistic);
        main_button_changeTraining = (Button) findViewById(R.id.ma_buttonChangeTraining);

        main_button_selectWordSet.setOnClickListener(new ButtonSelectSetOfWordsListener());
        main_button_goTraining.setOnClickListener(new ButtonGoTrainingListener());
        main_button_statistic.setOnClickListener(new ButtonStatisticListener());
        main_button_changeTraining.setOnClickListener(new ButtonChangeTrainingListener());

        main_textView_selectedSetOfWordName = (TextView) findViewById(R.id.ma_textViewSelectedSetOfWordName);
        main_textView_selectedTrainingName = (TextView) findViewById(R.id.ma_textViewSelectedTrainingName);

        // Show words of a day
        if (SettingsHelper.INSTANCE.isSOWSelected() && IS_RELEASE) {
            showWordsOfDayDialog();
        }



        updateInterface();
    }

    private void updateInterface() {
        if (SettingsHelper.INSTANCE.isSOWSelected()) {
            try {
                main_textView_selectedSetOfWordName.setText(getString(R.string.main_textView_SelectedSOW) + " " + SOWEngine.INSTANCE.getNameOfSelectedSetOfWords());
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            if (SettingsHelper.INSTANCE.isTrainingSelected()) {
                main_button_goTraining.setEnabled(true);
                main_button_goTraining.setOnClickListener(new ButtonGoTrainingListener());
                String currentTrainingKey = SettingsHelper.INSTANCE.getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID);
                main_textView_selectedTrainingName.setText(getResources().getString(R.string.main_menu_textView_selectedTraining) + " " + TrainingUtils.getNameByID(currentTrainingKey));
            } else {
                main_button_goTraining.setEnabled(true);
                main_button_goTraining.setOnClickListener(new ButtonChangeTrainingListener());
                main_textView_selectedTrainingName.setText(getString(R.string.main_string_trainingNotSelected));
            }
        } else {
            main_textView_selectedSetOfWordName.setText(getString(R.string.main_string_sowNotSelected));

            main_button_goTraining.setEnabled(false);
            main_textView_selectedTrainingName.setText(getString(R.string.main_string_selectSOWBefore));

            ReadyAnimations.animateView(this, main_button_selectWordSet, ReadyAnimations.ANIMATE_ROTATE_FLIPFLOP);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        //TextView tvInfo = (TextView) findViewById(R.id.tvInfo);

        // Операции для выбранного пункта меню
        switch (item.getItemId()) {
            case R.id.main_menu_settings:
               /* Intent intent = new Intent(instance(), GlobalSettings.class);
                startActivity(intent);
*/
                Intent intent = new Intent(this, GlobalSettings.class);
                startActivityForResult(intent, 1);

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

   /* private Context instance() {
        return this;
    }*/

    private void showWordsOfDayDialog() {
        new WordsOfDayDialog(this).show();
    }

    private class ButtonSelectSetOfWordsListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO temporary debug
            /*Intent intent = new Intent(instance(), SelectSetOfWords.class);
            startActivity(intent);*/

           /* Intent intent = new Intent(instance(), SelectLanguage.class);
            startActivity(intent);
            finish();*/

            Intent intent = new Intent(Main.this, SelectSetOfWords.class);
            startActivity(intent);
        }
    }

    private class ButtonGoTrainingListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = null;

            String currentTraining = SettingsHelper.INSTANCE.getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID);
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_ETR)) {
                intent = new Intent(Main.this, Training_ENG_to_RUS.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_GD)) {
                intent = new Intent(Main.this, Training_Guess_Description.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_PUWIC)) {
                intent = new Intent(Main.this, Training_Pick_up_word_in_context.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_SPRINT)) {
                intent = new Intent(Main.this, Training_Sprint.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_TW)) {
                intent = new Intent(Main.this, Training_TypeWriting.class);
            }

            if (intent == null) {
                Log.e(Main.LOG_TAG, "Training name error");
                return;
            }

            startActivity(intent);
        }
    }

    private class ButtonStatisticListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //To change body of implemented methods use File | Settings | File Templates.
            //Toast.makeText(instance(), "Нажата кнопка ОК", Toast.LENGTH_LONG).show();

           /* Animation anim = ReadyAnimations.loadAnimation(instance(), R.anim.anim_shake);
            v.startAnimation(anim);*/

            /*Intent intent = new Intent(instance(), SetOfWordOverview.class);
            Bundle b = new Bundle();
            b.putInt("key", 1); //Your id
            intent.putExtras(b); //Put your id to your next Intent
            startActivity(intent);*/
        }
    }

    private class ButtonChangeTrainingListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main.this, SelectTraining.class);
            startActivity(intent);
        }
    }
}